package com.yxmadoc.umlproject.service.impl;

import com.yxmadoc.umlproject.model.Repairer;
import com.yxmadoc.umlproject.repository.RepairerRepository;
import com.yxmadoc.umlproject.service.RepairerService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class RepairerServiceImpl implements RepairerService {

	@Autowired
	private RepairerRepository repository;

	@Override
	public Repairer save(Repairer repairer) {
		repairer.setRepairerPassword(DigestUtils.md5Hex(repairer.getRepairerPassword()));
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
		String dateString = sdf.format(date);
		repairer.setRepairerId("REPR" + dateString);
		return repository.save(repairer);
	}

	@Override
	public Repairer findOne(String repairerId) {
		return repository.findOne(repairerId);
	}
}
